package net.gamebookengine.viewer;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import net.gamebookengine.viewer.entities.Author;
import net.gamebookengine.viewer.entities.Gamebook;
import net.gamebookengine.viewer.entities.Section;
import net.gamebookengine.viewer.parser.InputParser;
import android.graphics.Bitmap;

public class GamebookService implements IGamebookService
{
	private static final String		START_SECTION	= "start";
	
	private Gamebook				gamebook;
	private HashMap<String, Bitmap>	images;
	
	public void init(InputStream in)
	{
		InputParser parser = new InputParser(in);
		gamebook = parser.getGamebook();
		images = parser.getImages();
	}
	
	public Author getAuthor()
	{
		return gamebook.getMeta().getAuthor();
	}
	
	public String getTitle()
	{
		return gamebook.getMeta().getTitle();
	}
	
	public String getSummary()
	{
		return gamebook.getMeta().getSummary();
	}
	
	public Section getSection(String id)
	{
		List<Section> sections = gamebook.getSections();
		for (Section section : sections)
		{
			if (id.equals(section.getId()))
			{
				return section;
			}
		}
		return null;
	}
	
	public String getStartSectionId()
	{
		List<Section> sections = gamebook.getSections();
		for (Section section : sections)
		{
			if (GamebookService.START_SECTION.equals(section.getType()))
			{
				return section.getId();
			}
		}
		return null;
	}
	
	public Bitmap getImage(String fileName)
	{
		return images.get(fileName);
	}

	public String getTheme() {
		return gamebook.getMeta().getTheme();
	}
	
}
